package com.cn.ccp.action.faculty;

import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.FacultyBean;
import com.cn.ccp.bean.SpecialtyBean;
import com.cn.ccp.service.faculty.FacultyService;
import com.cn.ccp.service.faculty.SpecialtyService;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.MsgConstants;

/**
 * 专业管理action
 * @author renyongcheng
 *
 */
public class SpecialtyAction extends BaseAction{
	private Logger log = Logger.getLogger(SpecialtyAction.class);
	private SpecialtyService specialtyService;
	private FacultyService facultyService;
    private SpecialtyBean specialtyBean;
	private List<SpecialtyBean> specialtyList;
    private String specialtyTitle,specialtyLabellName,specialtyLabelDescription,specialtyLabelFacutly;
    private SelectItem[] facultySelectItem ;//学院下拉框值
    private boolean disableBtn = true;
    
    public SpecialtyAction(){
    	super();
    	specialtyTitle = getMessages(MsgConstants.SPECIALTY_PAGE_TITLE);
    	specialtyLabellName = getMessages(MsgConstants.SPECIALTY_PAGE_LABEL_NAME);
    	specialtyLabelDescription = getMessages(MsgConstants.SPECIALTY_PAGE_LABEL_DES);
    	specialtyLabelFacutly = getMessages(MsgConstants.SPECIALTY_PAGE_LABEL_FACULTY);
    	WebApplicationContext fcontextutils = FacesContextUtils
				.getWebApplicationContext(FacesContext.getCurrentInstance());
    	specialtyService = (SpecialtyService) fcontextutils.getBean("specialtyService");
    	facultyService =  (FacultyService) fcontextutils.getBean("facultyService");

    	getpagedetail();
    }
     
 

	public List<SpecialtyBean> getSpecialtyList() {
		return specialtyList;
	}



	public void setSpecialtyList(List<SpecialtyBean> specialtyList) {
		this.specialtyList = specialtyList;
	}



	public String getSpecialtyTitle() {
		return specialtyTitle;
	}



	public void setSpecialtyTitle(String specialtyTitle) {
		this.specialtyTitle = specialtyTitle;
	}



	public String getSpecialtyLabellName() {
		return specialtyLabellName;
	}



	public void setSpecialtyLabellName(String specialtyLabellName) {
		this.specialtyLabellName = specialtyLabellName;
	}



	public String getSpecialtyLabelDescription() {
		return specialtyLabelDescription;
	}



	public void setSpecialtyLabelDescription(String specialtyLabelDescription) {
		this.specialtyLabelDescription = specialtyLabelDescription;
	}



	public String getSpecialtyLabelFacutly() {
		return specialtyLabelFacutly;
	}



	public void setSpecialtyLabelFacutly(String specialtyLabelFacutly) {
		this.specialtyLabelFacutly = specialtyLabelFacutly;
	}



 


	
	

	public SelectItem[] getFacultySelectItem() {
		List<FacultyBean> facultyList = facultyService.getAllFaculty();
		if(facultyList == null){
			facultySelectItem = new SelectItem[0];
		}else{
			facultySelectItem = new SelectItem[facultyList.size()];
			for(int i = 0 ; i < facultyList.size() ; i++){
				FacultyBean bean = facultyList.get(i);
				facultySelectItem[i] = new SelectItem(String.valueOf(bean.getId()) , bean.getFacultyName());
			}
		}
		log.info("---------"+facultySelectItem.length);
		return facultySelectItem;
	}



	public void setFacultySelectItem(SelectItem[] facultySelectItem) {
		this.facultySelectItem = facultySelectItem;
	}
 



	public SpecialtyBean getSpecialtyBean() {
		if(specialtyBean == null){
			specialtyBean = new SpecialtyBean();
			specialtyBean.setFacultyId(CCPContants.getUserBean().getFacultyId());
		}
		log.info("=="+specialtyBean);
		return specialtyBean;
	}

	public void setSpecialtyBean(SpecialtyBean specialtyBean) {
		this.specialtyBean = specialtyBean;
	}
	
	public void getpagedetail() {
		setItemCount(specialtyService.getCountSpecialty());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		specialtyList = specialtyService.pageQuerySpecialty(start, pagesize);
	}
	public String edit(){
		if(specialtyBean.getFacultyId().equals("-1")){
			showMsg("facultyId",  "javax.faces.validator.RequiredValidator.FAILED");
			return "";
		}
		if(specialtyBean.getSpecialtyname().equals("")){
			showMsg("specialtyname",  "javax.faces.validator.RequiredValidator.FAILED");
			return "";
		}
		log.info("---------------------------");
		boolean flag = specialtyService.specialtyNameIsAlready(specialtyBean.getId(), specialtyBean.getSpecialtyname(),specialtyBean.getFacultyId());
		if(flag){
			showMsg("specialtyname",  MsgConstants.SPECIALTY_PAGE_LABEL_SPECIALTY_NAME_FAILED);
			return "";
		}
		if(specialtyBean.getId() == 0)
			return save();
		return update();
	}
	public String save(){
		boolean flag = specialtyService.saveSpecialty(specialtyBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = specialtyService.updateSpecialty(specialtyBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}

	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		log.info("---------------------------------"+id);
		if(id != null)
			specialtyBean =  (SpecialtyBean)specialtyService.findSpecialtyById(Integer.parseInt(id));
		log.info("-----------------"+specialtyBean);
		return UPDATE;
	}
	
	
	
	public void doDelete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		specialtyService.delSpecialty(Integer.parseInt(id));
		getpagedetail();
		log.info("-----------------doDelete()----------------"+id);
	}



	public boolean isDisableBtn() {
		disableBtn = CCPContants.getUserBean().getUserName().equalsIgnoreCase("admin") ? false : true;
		return disableBtn;
	}

 	
}
